﻿
using BoriPeter.FatSecret.Converters;
using BoriPeter.FatSecret.Messages;
using BoriPeter.FitnessJournal8.Interfaces.DataSources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoriPeter.FatSecret
{
    public class FatSecretFoodDataSource : IFoodDataSource
    {
        public async Task<List<FitnessJournal8.Models.Food>> GetAllAsync()
        {
            if (FatSecretCommon.AccessToken != null)
            {
                var foodsGetFavorites = new FoodsGetFavorites();
                var response = await foodsGetFavorites.SendAsync();

                return response.Convert();
            }
            else
                return new List<FitnessJournal8.Models.Food>();
        }

        public async Task<FitnessJournal8.Models.Food> GetByIdAsync(int _id)
        {
            if (FatSecretCommon.AccessToken != null)
            {
                var getFoodMessage = new FoodGet(_id);
                var food = await getFoodMessage.SendAsync();
                return food.Convert();
            }
            else
                return new FitnessJournal8.Models.Food();
        }

        public Task<FitnessJournal8.Models.Food> GetWhereAsync(Func<FitnessJournal8.Models.Food, bool> _expression)
        {
            throw new NotImplementedException();
        }

        public Task SaveAllAsync(List<FitnessJournal8.Models.Food> _list)
        {
            throw new NotImplementedException();
        }

        public async Task SaveAsync(FitnessJournal8.Models.Food _food)
        {
            if (FatSecretCommon.AccessToken != null)
            {
                if (_food.NewEntry != null)
                {
                    var foodEntryCreateMessage = new FoodEntryCreate(
                        _food.Id,
                        _food.NewEntry.EntryName,
                        _food.SelectedServing.Id,
                        _food.NewEntry.NumberOfUnits,
                        _food.NewEntry.Date,
                        _food.NewEntry.Meal
                        );
                    var response = await foodEntryCreateMessage.SendAsync();
                    _food.NewEntry.Id = response.FoodEntryId;
                    if (_food.Entries == null)
                        _food.Entries = new System.Collections.ObjectModel.ObservableCollection<FitnessJournal8.Models.FoodEntry>();
                    _food.Entries.Add(_food.NewEntry);
                    _food.NewEntry = null;
                }
                if (_food.FavoriteNotStored)
                {
                    if (_food.Favorite)
                    {
                        var foodAddFavoriteMessage = new FoodAddFavorite(_food.Id);
                        await foodAddFavoriteMessage.SendAsync();
                    }
                    else
                    {
                        var foodDeleteFavorite = new FoodDeleteFavorite(_food.Id);
                        await foodDeleteFavorite.SendAsync();
                    }
                    _food.FavoriteNotStored = false;
                }
            }
            else
                return;
        }
    }
}
